package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.net.retrofit.BaseRxObserver;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.RxImageTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.HomeFindBean;
import com.jz.community.moduleshoppingguide.home.bean.ShareBean;
import com.jz.community.moduleshoppingguide.home.model.ShoppingModelGuideImpl;
import com.jz.community.moduleshoppingguide.home.ui.activity.BestSellersActivity;
import com.jz.community.moduleshoppingguide.home.ui.activity.PreferentialActivity;
import com.jz.community.moduleshoppingguide.home.utils.HomeConst;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopActivity;
import com.jz.community.moduleshoppingguide.neighbor.ui.activity.NeighborMainActivity;

import java.util.List;


public class HomeMainActOneAdapter extends BaseQuickAdapter<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean, BaseViewHolder> {

    public HomeMainActOneAdapter(int layoutResId, @Nullable List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean entity) {
        ImageView imageView = helper.getView(R.id.home_main_one_iv);
        RoundCornersTransformation transformation = new RoundCornersTransformation(mContext,
                RxImageTool.dp2px(5), RoundCornersTransformation.CornerType.ALL);
        GlideApp.with(mContext)
                .load(entity.getImage())
                .transform(transformation)
                .placeholder(R.mipmap.default_img_2_1)
                .error(R.mipmap.default_img_2_1)
                .into(imageView);

        if (!Preconditions.isNullOrEmpty(entity.getLabel())){
            SHelper.vis(helper.getView(R.id.home_subject_layout));
            helper.setText(R.id.home_main_one_title, entity.getLabel());
        }else {
            SHelper.gone(helper.getView(R.id.home_subject_layout));
        }

        helper.setText(R.id.home_main_one_title_son, entity.getFtitle());
        helper.setText(R.id.home_main_one_title_describe, entity.getZtitle());

        LinearLayout goods_parent = helper.getView(R.id.home_main_one_parent);

        ViewGroup.LayoutParams lp1 = goods_parent.getLayoutParams();
        int parent_width = 0;

        if (getItemCount() > 1) {
            parent_width = ((SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(48)));
        } else {
            parent_width = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(20));
        }

        lp1.width = parent_width;
        goods_parent.setLayoutParams(lp1);
        ViewGroup.LayoutParams lp2 = imageView.getLayoutParams();
        lp2.width = parent_width;
        lp2.height = parent_width / 2;
        imageView.setLayoutParams(lp2);
        imageView.setOnClickListener(v -> {
            if (HomeConst.SPECIAL_URL.equals(entity.getJumpType())) {
                mContext.startActivity(new Intent(mContext, BaseX5WebActivity.class)
                        .putExtra("url", entity.getJumpParameter())
                        .putExtra("title", entity.getUrlTitle()));
            } else if (HomeConst.HOT_SALE.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, BestSellersActivity.class);
                intent.putExtra("message", 1);
                intent.putExtra("name", "热卖推荐");
                mContext.startActivity(intent);
            } else if (HomeConst.COMMODITY_DETAILS.equals(entity.getJumpType())) {
                ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", entity.getGoodsLink())
                        .navigation();
            } else if (HomeConst.NEIGHBOR.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, NeighborMainActivity.class);
                mContext.startActivity(intent);
            } else if (HomeConst.CARD_MORE.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.CARD_HOME);
            } else if (HomeConst.PAY_THE_PHONE_BILL.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.RECHARGE_PHONE);
            } else if (HomeConst.DISCOUNT_SPECIAL.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, PreferentialActivity.class);
                mContext.startActivity(intent);
            } else if (HomeConst.INTEGRAL_SHOP.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.INTEGRAL_GOODS);
            } else if (HomeConst.INVITATION_GIFT.equals(entity.getJumpType())) {
                new ShoppingModelGuideImpl(mContext).initShareGift(new BaseRxObserver<ShareBean>() {
                    @Override
                    public void onSuccess(ShareBean shareBean) {
                        mContext.startActivity(new Intent(mContext, BaseX5WebActivity.class)
                                .putExtra("url", shareBean.getData())
                                .putExtra("title", "分享有礼"));
                    }
                });
            } else if (HomeConst.NEARSHOP.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, NearShopActivity.class);
                mContext.startActivity(intent);
            }
        });


    }
}
